 define(function (){

	var el
	var opt
	var el_step_panel
	var el_content_panel
	var step_array
	var current_step
	var el_section

	var c_prev = "prev"
	var c_next = "next"

	return {
		init : function (paramEl ,paramOpt){
			var self = this
			el = paramEl
			opt = paramOpt
			step_array = opt.step_array
			el_step_panel = $('<div class="__step_panel"></div>').appendTo(el)
			el_content_panel = $('<div class="__content_panel"></div>').appendTo(el)
			self.__step_span_render()
			self.__step_content_render()
		},
		__step_span_render : function (){
			var stepTmp
			var disable
			var isLast
			for (var i = 0; i < step_array.length; i++) {
				stepTmp = step_array[i]
				isLast = (i == (step_array.length - 1)?true:false)
				$('<div class="__step_div"><div class="__step_span">'+(i+1)+'</div><span>'
					+stepTmp.name+'</span></div>').appendTo(el_step_panel)
				!isLast && $('<div class="__line"></div>').appendTo(el_step_panel)
			}
		},
		__step_content_render : function (){
			var self = this
			el_section = el.find("section").clone()
			el.find("section").remove()
			var el_content;
			el_section.children().each(function (i){
				el_content = $('<div class="__content"></div>').appendTo(el_content_panel)
			})
			self.__step_info_render()
			self.__step_foot_render()
		},
		__step_info_render : function (){
			var el_content = el_content_panel.children()
			var el_section_list = el_section.children()
			var el_content_info
			el_step_panel.find("[class*=__step_span]").each(function (i){
				el_content_info = $('<div class="__info">'+el_section_list.eq(i).html()+'</div>').appendTo(el_content.eq(i))
			})
		},
		__step_foot_render : function (){
			var self = this
			var el_content = el_content_panel.children()
			var el_content_foot
			var el_prev
			var el_next
			var el_finish
			var el_step_panel_length = el_step_panel.find("[class*=__step_span]").size()
			el_step_panel.find("[class*=__step_span]").each(function (i){
				el_content_foot = $('<div class="__foot"></div>').appendTo(el_content.eq(i))
				if (i !=0 )
					el_prev = $('<div class="__prev">上一步</div>').appendTo(el_content_foot),
					el_prev.click(function (){self.__step_foot_click(this ,c_prev)})
				if (i != el_step_panel_length-1)
					el_next = $('<div class="__next">下一步</div>').appendTo(el_content_foot),
					el_next.click(function (){self.__step_foot_click(this ,c_next)})
				if (i == el_step_panel_length-1)
					el_finish = $('<div class="__next">完成</div>').appendTo(el_content_foot),
					el_finish.click(function (){opt.finish()})
			})
		},
		__step_foot_click : function (el_step_direction ,direction){
			var self = this
			if (direction == c_next){
				if ( (step_array[current_step].validate && step_array[current_step].validate()) ||
						!step_array[current_step].validate)
				{
					el_step_panel.find("[class*=__step_span]").eq(current_step).addClass("finish").removeClass("active")
					self.step_show(++current_step)
				}
			}
			else
				self.step_show(--current_step)
		},
		step_show : function (step){
			opt.beforeRender && opt.beforeRender(step)
			current_step = step
			var el_current_step = el_step_panel.find("[class*=__step_span]").eq(current_step)
			el_current_step.addClass("active").removeClass("finish").siblings().removeClass("active")
			var el_current_content = el_content_panel.find("[class*=__content]").eq(current_step)
			el_current_content.addClass("active").removeClass("finish").siblings().removeClass("active")
			if (!el_current_step.data("isInit"))
				step_array[step].render && step_array[step].render(el_current_content),el_current_step.data("isInit" ,true)
		},
		finish_set : function (step){
			el_step_panel.find("[class*=__step_span]").eq(step).addClass("finish")
		},
		step_reset : function (step){
			el_step_panel.find("[class*=__step_span]").eq(step).data("isInit" ,false)
			el_step_panel.find("[class*=__step_span]").eq(step).removeClass("finish")
		}
	}
});